package com.szl.yeb.controller;

import com.szl.yeb.dto.ChatMsg;
import com.szl.yeb.entity.Admin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @program: yeb
 * @description: WebSocket服务接口
 * @author: szl
 * @create: 2022-09-02 15:29
 **/
@Api(tags = "WebSocket服务接口(WebSocketController)")
@RestController
public class WebSocketController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    @ApiOperation("")
    public void handleMessage(Authentication authentication, ChatMsg chatMsg) {
        Admin admin = (Admin) authentication.getPrincipal();
        chatMsg
                .setFrom(admin.getUsername())
                .setFromNickName(admin.getName())
                .setDate(LocalDateTime.now());

        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);

    }
}
